﻿-- Author      : Kurapica

--------------------------
-- PopupDialog      --
--------------------------
do
	-- Check Version
	local version = 1

	if not IGAS:RegisterFile("IGAS.GUI.Widget.PopupDialog", version) then
		return
	end

	local pairs = pairs
	local ipairs = ipairs
	local type = type
	local error = error
	local wipe = table.wipe
    local CreateFrame = IGAS.GUI.CreateFrame

	local _WidgetName = "PopupDialog"
	local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
	local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType
	
	local _ShowPopup
	-- Only need one table to help set button's pos.
	local _CheckBtn = {}

	-- Script
	local _FrameBackdrop = {
		bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 16, edgeSize = 16,
		insets = { left = 3, right = 3, top = 3, bottom = 3 }
	}

	local function OnMouseDown(self)
		self:StartMoving()
	end

	local function OnMouseUp(self)
		self:StopMovingOrSizing()
	end

	local function CloseButtonClick(self)
		self:GetParent():Hide()
		self.Parent:Fire("OnCancel")
	end
	
	local function Okay_OnClick(self)
		self.Parent:Hide()
		self.Parent:Fire("OnOkay")
	end
	
	local function No_OnClick(self)
		self.Parent:Hide()
		self.Parent:Fire("OnNo")
	end
	
	local function Cancel_OnClick(self)
		self.Parent:Hide()
		self.Parent:Fire("OnCancel")
	end
	
	local function OnTextChanged(self)
		self.Parent:Fire("OnTextChanged")
	end

	local function OnEnterPressed(self)
		self.Parent:Hide()
		self.Parent:Fire("OnOkay")
	end
	
	local function OnEscapePressed(self)
		self.Parent:Hide()
		self.Parent:Fire("OnCancel")
	end
		
	local function resize(self)
		local width, height
	
		local width = 320
		if self:GetChild("OkayBtn").Visible and self:GetChild("NoBtn").Visible and self:GetChild("CancelBtn").Visible then
			width = 440
		elseif self:GetChild("AlertIcon").Visible then
			width = 420
		end
		
		if self:GetChild("InputTxt").Visible then
			height = 16 + self:GetChild("AlertText").Height + 8 + self:GetChild("InputTxt").Height + 8 + self:GetChild("OkayBtn").Height + 16
		else
			height = 16 + self:GetChild("AlertText").Height + 8 + self:GetChild("OkayBtn").Height + 16
		end
		
		self.Width = width
		self.Height = height
	end
	
	local function OnShow(self)		
		if ( UnitIsDeadOrGhost("player") and not self.ShowWhileDead ) then
			self:Hide()
			self:Fire("OnCancel")
			return
		end

		if ( InCinematic() and not self.ShowWhileCinematic ) then
			self:Hide()
			self:Fire("OnCancel")
			return
		end
		
		-- Check if last popup is shown
		if _ShowPopup and _ShowPopup ~= self then
			_ShowPopup:Fire("OnCancel")
			_ShowPopup:Hide()
		end
		_ShowPopup = self
				
		resize(self)
		
		wipe(_CheckBtn)
		
		-- Set buttons position
		self:GetChild("OkayBtn"):ClearAllPoints()
		self:GetChild("NoBtn"):ClearAllPoints()
		self:GetChild("CancelBtn"):ClearAllPoints()
		
		if self:GetChild("OkayBtn").Visible then
			table.insert(_CheckBtn, self:GetChild("OkayBtn"))
		end
		
		if self:GetChild("NoBtn").Visible  then
			table.insert(_CheckBtn, self:GetChild("NoBtn"))
		end
		
		if self:GetChild("CancelBtn").Visible then
			table.insert(_CheckBtn, self:GetChild("CancelBtn"))
		end
		
		if _CheckBtn[3] then
			if self:GetChild("InputTxt").Visible then
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("InputTxt"), "BOTTOM", -72, -8)
				_CheckBtn[2]:SetPoint("LEFT", _CheckBtn[1], "RIGHT", 13, 0)
				_CheckBtn[3]:SetPoint("LEFT", _CheckBtn[2], "RIGHT", 13, 0)
			else
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("AlertText"), "BOTTOM", -72, -8);
				_CheckBtn[2]:SetPoint("LEFT", _CheckBtn[1], "RIGHT", 13, 0)
				_CheckBtn[3]:SetPoint("LEFT", _CheckBtn[2], "RIGHT", 13, 0)
			end
		elseif _CheckBtn[2] then
			if self:GetChild("InputTxt").Visible then
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("InputTxt"), "BOTTOM", -6, -8)
				_CheckBtn[2]:SetPoint("LEFT", _CheckBtn[1], "RIGHT", 13, 0)
			else
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("AlertText"), "BOTTOM", -6, -8)
				_CheckBtn[2]:SetPoint("LEFT", _CheckBtn[1], "RIGHT", 13, 0)
			end
		elseif _CheckBtn[1] then
			if self:GetChild("InputTxt").Visible then
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("InputTxt"), "BOTTOM", 0, -8)
			else
				_CheckBtn[1]:SetPoint("TOPRIGHT", self:GetChild("AlertText"), "BOTTOM", 0, -8)
			end
		end
	end
	
	local function OnHide(self)
		if _ShowPopup == self then
			_ShowPopup = nil
		end
	end
	
	-- ScriptType
	local _ScriptType = {
		["OnCancel"] = true,
		["OnNo"] = true,
		["OnOkay"] = true,
		["OnTextChanged"] = true,
	}

	--	FuncProxy
	local _FuncProxy = {
	}

	--	Property
	local _Property = {
		-- Message
		["Message"] = {
			["Set"] = function(self, text)
				self:GetChild("AlertText").Text = text or ""
			end,
			
			["Type"] = _PropertyType.String,
		},
		-- Text
		["Text"] = {
			["Set"] = function(self, text)
				self:GetChild("InputTxt").Text = text or ""
			end,
			
			["Get"] = function(self)
				return self:GetChild("InputTxt").Text
			end,
			
			["Type"] = _PropertyType.String,
		},
		-- OkayText
		["OkayButtonText"] = {
			["Set"] = function(self, text)
				self:GetChild("OkayBtn").Text = text or "Okay"
				local width = self:GetTextWidth()
				if ( width > 110 ) then
					self:SetWidth(width + 20)
				else
					self:SetWidth(120);
				end
			end,
			
			["Type"] = _PropertyType.String,
		},
		-- NoText
		["NoButtonText"] = {
			["Set"] = function(self, text)
				self:GetChild("NoBtn").Text = text or "No"
				local width = self:GetTextWidth()
				if ( width > 110 ) then
					self:SetWidth(width + 20)
				else
					self:SetWidth(120);
				end
			end,
			
			["Type"] = _PropertyType.String,
		},
		-- CancelText
		["CancelButtonText"] = {
			["Set"] = function(self, text)
				self:GetChild("CancelBtn").Text = text or "Cancel"
				local width = self:GetTextWidth()
				if ( width > 110 ) then
					self:SetWidth(width + 20)
				else
					self:SetWidth(120);
				end
			end,
			
			["Type"] = _PropertyType.String,
		},
		-- ShowAlertIcon
		["ShowAlertIcon"] = {
			["Set"] = function(self, flag)
				self:GetChild("AlertIcon").Visible = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self:GetChild("AlertIcon").Visible
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowOkayButton
		["ShowOkayButton"] = {
			["Set"] = function(self, flag)
				self:GetChild("OkayBtn").Visible = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self:GetChild("OkayBtn").Visible
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowNoButton
		["ShowNoButton"] = {
			["Set"] = function(self, flag)
				self:GetChild("NoBtn").Visible = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self:GetChild("NoBtn").Visible
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowCancelButton
		["ShowCancelButton"] = {
			["Set"] = function(self, flag)
				self:GetChild("CancelBtn").Visible = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self:GetChild("CancelBtn").Visible
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowInputBox
		["ShowInputBox"] = {
			["Set"] = function(self, flag)
				self:GetChild("InputTxt").Visible = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self:GetChild("InputTxt").Visible
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowWhileDead
		["ShowWhileDead"] = {
			["Set"] = function(self, flag)
				self.__ShowWhileDead = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return self.__ShowWhileDead or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
		-- ShowWhileCinematic
		["ShowWhileCinematic"] = {
			["Set"] = function(self, flag)
				self.__ShowWhileCinematic = (flag and true) or false
			end,
			
			["Get"] = function(self)
				return (self.__ShowWhileCinematic and true) or false
			end,
			
			["Type"] = _PropertyType.Boolean,
		},
	}

	local function _New(parent)		
		-- New Frame
		local frame = _GUI.CreateFrame("Frame",nil,parent)

		frame.Width = 320
		frame.Height = 72
		frame.MouseEnabled = true
        frame.Movable = true
		frame.FrameStrata = "DIALOG"
		frame.Visible = false
		frame.Toplevel = true  

		frame:SetPoint("CENTER",UIParent,"CENTER",0,0)
		--frame:ClearAllPoints()
		frame:SetBackdrop(_FrameBackdrop)
		frame:SetBackdropColor(0,0,0,1)
		frame:SetMinResize(400,200)

		-- Layers
		local text = CreateFrame("FontString", "AlertText", frame, "ARTWORK", "GameFontHighlight")
		text.Width = 290
		text.Height = 0
		text:SetPoint("TOP", frame, "TOP", 0, -16)
		
		local icon = CreateFrame("Texture", "AlertIcon", frame, "ARTWORK")
		icon:SetTexture("Interface\\DialogFrame\\DialogAlertIcon")
		icon.Height = 64
		icon.Width = 64
		icon.Visible = false
		icon:SetPoint("LEFT", frame, "LEFT", 12, 0)

		-- Close Button		
		local btnClose = CreateFrame("Button","CloseBtn",frame)
		btnClose:SetPoint("TOPRIGHT",frame,"TOPRIGHT")
        btnClose.Style = "CLOSE"
		btnClose.OnClick = CloseButtonClick

		-- Okay Button
		local btnOkay = CreateFrame("Button", "OkayBtn", frame)
		btnOkay.Style = "NORMAL"
		btnOkay.Height = 20
		btnOkay.Width = 128
		btnOkay.ID = 1
		btnOkay.Text = "Okay"
		
		-- No Button
		local btnNo = CreateFrame("Button", "NoBtn", frame)
		btnNo.Style = "NORMAL"
		btnNo.Height = 20
		btnNo.Width = 128
		btnNo.ID = 2
		btnNo.Text = "No"

		-- Cancel Button
		local btnCancel = CreateFrame("Button", "CancelBtn", frame)
		btnCancel.Style = "NORMAL"
		btnCancel.Height = 20
		btnCancel.Width = 128
		btnCancel.ID = 3
		btnCancel.Text = "Cancel"
		
		-- SingleTextBox
		local txtInput = CreateFrame("SingleTextBox", "InputTxt", frame)
		txtInput.Height = 32
		txtInput.Width = 130
		txtInput.HistoryLines = 1
		txtInput.Visible = false
		txtInput:SetPoint("BOTTOM", frame, "BOTTOM", 0, 45)
		
		frame:StackScript("OnMouseDown", OnMouseDown)
		frame:StackScript("OnMouseUp", OnMouseUp)
		frame:StackScript("OnShow", OnShow)
		frame:StackScript("OnHide", OnHide)
		
		btnOkay.OnClick = Okay_OnClick	
		btnNo.OnClick = No_OnClick	
		btnCancel.OnClick = Cancel_OnClick
	
		txtInput.OnTextChanged = OnTextChanged
		txtInput.OnEnterPressed = OnEnterPressed
		txtInput.OnEscapePressed = OnEscapePressed
	
		return frame
	end

	-- Register
	local _WidgetInfo = {
		["WidgetName"] = _WidgetName,
		["Base"] = _Base,
		["FuncProxy"] = _FuncProxy,
		["Property"] = _Property,
		["ScriptType"] = _ScriptType,
		["New"] = _New,
	}

	_Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)
end
